package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.api.ApiConstant;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.CommonSearchBean;
import com.fy.fyzf.bean.HomeBannerBean;
import com.fy.fyzf.bean.HousesBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.HousingCountBean;
import com.fy.fyzf.presenter.HouseSoucePresenter;
import com.fy.fyzf.utils.ShareHelper;
import com.fy.fyzf.view.HouseSouceView;
import com.fy.fyzf.view.SpacesItemDecoration;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.adapter.HousesListAdapter;

public class AllBuildingActivity extends BaseActivity<HouseSoucePresenter> implements HouseSouceView {

    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private HousesListAdapter housesListAdapter;

    @Override
    protected HouseSoucePresenter createPresenter() {
        return new HouseSoucePresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_all_building;
    }
    private int page = 1;
    private int tag = 1;
    private int pageSize = 10;
    private ShareHelper shareHelper = new ShareHelper(this);

    private HousesBean commonSearchBean = new HousesBean();
    @Override
    public void initView() {
        super.initView();

        setTitleName("全部房源");
        housesListAdapter = new HousesListAdapter(this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.addItemDecoration(new SpacesItemDecoration(16));
        recycleView.setAdapter(housesListAdapter);


        housesListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean.ListBean> listBeans = adapter.getData();
                Intent intent = new Intent(AllBuildingActivity.this
                        , BuildingDetailActivity.class);
                intent.putExtra("housingid", listBeans.get(position).getFloorId());
                intent.putExtra("type", 1);
                ImageView imageView = view.findViewById(R.id.iv_logo);
                ActivityOptionsCompat options = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(AllBuildingActivity.this,
                                imageView, "logo");
                startActivity(intent, options.toBundle());
            }
        });
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                tag = 1;
                commonSearchBean.setPageNum(page);
                commonSearchBean.setPageSize(pageSize);
                initData();
            }
        });
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                tag = 2;
                commonSearchBean.setPageNum(page);
                commonSearchBean.setPageSize(pageSize);
                initData();
            }
        });

        housesListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean.ListBean> data = adapter.getData();
                String url = ApiConstant.BASE_SERVER_URL + "shareHousing/getHousing?housingId="+data.get(position).getHousingId();
                if (view.getId() == R.id.share_wx) {
                    shareHelper.shareWx(data.get(position).getFloorImage(), url);
                } else if (view.getId() == R.id.share_pyq) {
                    shareHelper.shareWechatMoments(data.get(position).getFloorImage(),url);
                }
            }
        });
        shareHelper.setShareListener(new ShareHelper.onShareListener() {
            @Override
            public void shareSuccess() {
                mPresenter.addIntegral();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getHouseList(commonSearchBean);
    }

    @Override
    public void getHouseSuccess(HousesListBean housesListBean) {
        smartRefresh.finishLoadMore();
        smartRefresh.finishRefresh();
        if (housesListBean.getList().size() <= 0 || null == housesListBean) {
            housesListAdapter.setEmptyView(R.layout.layout_nodata_search, recycleView);
        }
        if (tag == 1) {
            housesListAdapter.setNewData(housesListBean.getList());
        } else {
            housesListAdapter.addData(housesListBean.getList());
        }

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> areaListBeans) {

    }

    @Override
    public void getSubwatdataSuccess(List<AreaListBean> areaListBeans) {

    }

    @Override
    public void onError() {

    }

    @Override
    public void getMatchHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void getFloorCountSuccess(HousingCountBean housingCountBean) {

    }

    @Override
    public void getBannerSuccess(List<HomeBannerBean> homeBannerBeans) {

    }
}